#include "fwModelTextStore.h"
#include "..\fwLog.h"

Vector2 ModelTextHelper::ReadVector2(FILE* f, const char* format)
{
	Vector2 vector;
	fscanf(f, format, &vector.x, &vector.y);
	return vector;
}
Vector3 ModelTextHelper::ReadVector3(FILE* f, const char* format)
{
	Vector3 vector;
	fscanf(f, format, &vector.x, &vector.y, &vector.z);
	return vector;
}
Vector4 ModelTextHelper::ReadVector4(FILE* f, const char* format)
{
	Vector4 vector;
	fscanf(f, format, &vector.x, &vector.y, &vector.z, &vector.w);
	return vector;
}
void ModelTextHelper::WriteVector2(FILE* f, const char* format, Vector2 vector)
{
	fprintf(f, format, vector.x, vector.y);
}
void ModelTextHelper::WriteVector3(FILE* f, const char* format, Vector3 vector)
{
	fprintf(f, format, vector.x, vector.y, vector.z);
}
void ModelTextHelper::WriteVector4(FILE* f, const char* format, Vector4 vector)
{
	fprintf(f, format, vector.x, vector.y, vector.z, vector.w);
}
Vertex* ModelTextHelper::ReadVertices(FILE* f, const FWuint count)
{
	Vertex* vertices = new Vertex[count];
	for (FWuint i = 0; i < count; i++)
	{
		Vertex vertex;
		vertex.Position = ReadVector3(f, POSITION_STRING);
		vertex.Coordinate = ReadVector2(f, COORDINATE_STRING);
		vertex.Normal = ReadVector3(f, NORMAL_STRING);
		vertex.BiNormal = ReadVector3(f, BINORMAL_STRING);
		vertex.Tangent = ReadVector3(f, TANGENT_STRING);
		vertices[i] = vertex;
	}
	return vertices;
}
FWuint* ModelTextHelper::ReadIndices(FILE* f, const FWuint count)
{
	FWuint indexCount = count * 3;
	FWuint* indices = new FWuint[indexCount];
	for (FWuint i = 0; i < indexCount; i += 3)
	{
		fscanf(f, INDEX_STRING, &indices[i], &indices[i + 1], &indices[i + 2]);
	}

	return indices;
}
void ModelTextHelper::WriteVertices(FILE* f, const FWuint count, Vertex* vertices)
{
	fprintf(f, NUM_STRING, count);
	for (FWuint i = 0; i < count; i++)
	{
		WriteVector3(f, POSITION_STRING, vertices[i].Position);
		WriteVector2(f, COORDINATE_STRING, vertices[i].Coordinate);
		WriteVector3(f, NORMAL_STRING, vertices[i].Normal);
		WriteVector3(f, BINORMAL_STRING, vertices[i].BiNormal);
		WriteVector3(f, TANGENT_STRING, vertices[i].Tangent);
	}
}
void ModelTextHelper::WriteIndices(FILE* f, const FWuint count, FWuint* indices)
{
	fprintf(f, NUM_STRING, count / 3);
	for (FWuint i = 0; i < count; i += 3)
		fprintf(f, "%d %d %d\n", indices[i], indices[i + 1], indices[i + 2]);
}